#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名：09-file.py

print "写入内容"
foo = open("foo.lua", "w+")
print dir(foo)
print "文件名{0}".format(foo.name)
print "文件模式{0}".format(foo.mode)
print "是否关闭{0}".format(foo.closed)

foo.write("hello \n")
foo.write("lua")

foo.close()

print "简单模式一次读取所有内容"
foo = open("foo.lua", "r")
print foo.read()
foo.close()

print "常用模式以多行模式读取所有内容"
foo = open("foo.lua", "r")
for line in foo.readlines():
    print line.strip()
    
foo.close()

# == == 生产中 == =
print "===生产中read start==="

#filename = 'pi_digits.txt'
filename = 'programming.txt'

# 以with方式打开不需要close，会自动关闭
with open(filename) as file_object:
    lines = file_object.readlines()

# 得到一个列表
print type(lines)
print "打印lines{0}".format(lines)
print "==="

# 遍历这个列表
for line in lines:
    # 打印列表项
    # print line
    #print line.rstrip()
    if line.strip() == "" or line.startswith("#"):
        print "empty"
        continue
    else:
        print line.strip()
# 查找包含ph的列表项
# if 'ph' in line:
        # 显示具体一项
        # 把一项以=分成列表
       # print line.strip().split("=")
        
        # print line.strip().split("=")[0]

    # else:
       # print "找不到"

print "===read end==="


print "===生产中write start==="

filename = 'programming.txt'

# 以w模式打开，如果没有该文件，会创建。如果有，则覆盖
#  以a 模式打开，追加内容

'''with open(filename, 'w') as file_object:
    file_object.write("c++ \n")
    file_object.write("php \n")'''
